<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="会员账号" prop="memberAccount">
        <el-input
            v-model="queryParams.memberAccount"
            placeholder="请输入所属会员"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动名称" prop="activityName">
        <el-input
            v-model="queryParams.activityName"
            placeholder="请输入活动名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="兑换码" prop="awardCode">
        <el-input
            v-model="queryParams.awardCode"
            placeholder="请输入兑换码"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收件人姓名" prop="recipientName">
        <el-input
            v-model="queryParams.recipientName"
            placeholder="请输入收件人姓名"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收件人手机号" prop="recipientPhone">
        <el-input
            v-model="queryParams.recipientPhone"
            placeholder="请输入收件人手机号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="快递单号" prop="recipientOrder">
        <el-input
            v-model="queryParams.recipientOrder"
            placeholder="请输入快递单号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['member:memberTrainAward:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['member:memberTrainAward:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['member:memberTrainAward:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['member:memberTrainAward:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="memberTrainAwardList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="所属会员" align="center" width="140" show-overflow-tooltip prop="memberName"/>
      <el-table-column label="活动名称" align="center" width="180" show-overflow-tooltip prop="activityName"/>
      <el-table-column label="兑换码" align="center" width="180" prop="awardCode"/>
      <el-table-column label="奖品等级" align="center" width="140" prop="awardLevel"/>
      <el-table-column label="奖品说明" align="center" width="180" show-overflow-tooltip prop="awardContent"/>
      <el-table-column label="是否兑奖" align="center" prop="awardStatus">
        <template #default="scope">
          {{ scope.row.awardStatus == 0 ? '未兑换' : '已兑换' }}
        </template>
      </el-table-column>
      <el-table-column label="兑换时间" align="center" prop="redeemTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.redeemTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注说明" align="center" width="180" show-overflow-tooltip prop="awardRemark"/>
      <el-table-column label="收件人姓名" align="center" width="160" prop="recipientName"/>
      <el-table-column label="收件人手机号" align="center" width="180" prop="recipientPhone"/>
      <el-table-column label="收件地址" align="center" width="180" show-overflow-tooltip prop="recipientAddress"/>
      <el-table-column label="快递单号" align="center" width="180" prop="recipientOrder"/>
      <el-table-column label="操作" align="center" fixed="right" width="200" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
              type="text"
              icon="Edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['member:memberTrainAward:edit']"
          >修改
          </el-button>
          <el-button
              type="text"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['member:memberTrainAward:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改会员奖品对话框 -->
    <el-dialog :title="title" v-model="open" width="70%" :close-on-click-modal="false" append-to-body>
      <el-form ref="memberTrainAwardRef" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="所属会员" prop="memberId">
              <el-input v-model="form.memberId" placeholder="请选择所属会员"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属活动" prop="activityId">
              <el-input v-model="form.activityId" placeholder="请选择所属活动"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="兑换码" prop="awardCode">
              <el-input v-model="form.awardCode" placeholder="请输入兑换码"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="奖品等级" prop="awardLevel">
              <el-input v-model="form.awardLevel" placeholder="请输入奖品等级"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="奖品说明">
              <el-input v-model="form.awardContent" type="textarea" placeholder="请输入奖品等级"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="备注说明" prop="awardRemark">
              <el-input v-model="form.awardRemark" type="textarea" placeholder="请输入备注说明"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="是否兑换" prop="awardStatus">
              <el-switch v-model="form.awardStatus" :active-value="1" :inactive-value="0"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="兑换时间" prop="redeemTime">
              <el-date-picker clearable
                              v-model="form.redeemTime"
                              type="datetime"
                              value-format="YYYY-MM-DD HH:mm:ss"
                              placeholder="请选择兑换时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="姓名" prop="recipientName">
              <el-input v-model="form.recipientName" placeholder="请输入收件人姓名"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="recipientPhone">
              <el-input v-model="form.recipientPhone" placeholder="请输入收件人手机号"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="地址" prop="recipientAddress">
              <el-input v-model="form.recipientAddress" placeholder="请输入收件地址"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="快递单号" prop="recipientOrder">
              <el-input v-model="form.recipientOrder" placeholder="请输入快递单号"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="MemberTrainAward">
import {
  listMemberTrainAward,
  getMemberTrainAward,
  delMemberTrainAward,
  addMemberTrainAward,
  updateMemberTrainAward
} from "@/api/member/memberTrainAward";

const {proxy} = getCurrentInstance();

const memberTrainAwardList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    memberId: null,
    activityId: null,
    activityName: null,
    awardCode: null,
    awardLevel: null,
    awardContent: null,
    awardStatus: null,
    redeemTime: null,
    awardRemark: null,
    recipientName: null,
    recipientPhone: null,
    recipientAddress: null,
    recipientOrder: null,
  },
  rules: {}
});

const {queryParams, form, rules} = toRefs(data);

/** 查询会员奖品列表 */
function getList() {
  loading.value = true;
  listMemberTrainAward(queryParams.value).then(response => {
    memberTrainAwardList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    memberId: null,
    activityId: null,
    activityName: null,
    awardCode: null,
    awardLevel: null,
    awardContent: null,
    awardStatus: 0,
    redeemTime: null,
    awardRemark: null,
    recipientName: null,
    recipientPhone: null,
    recipientAddress: null,
    recipientOrder: null,
    createTime: null,
    createBy: null,
    updateTime: null,
    updateBy: null,
    delFlag: null
  };
  proxy.resetForm("memberTrainAwardRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加会员奖品";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getMemberTrainAward(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改会员奖品";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["memberTrainAwardRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateMemberTrainAward(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMemberTrainAward(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除会员奖品编号为"' + _ids + '"的数据项？').then(function () {
    return delMemberTrainAward(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('member/memberTrainAward/export', {
    ...queryParams.value
  }, `memberTrainAward_${new Date().getTime()}.xlsx`)
}

getList();
</script>
